/*
Simple Forum Auto Poster.
Copyright (C) 2011 Gianni Van Hoecke.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package gui.loading;

import domain.DomainController;
import domain.settings.Settings;
import domain.ui.LoadingUI;
import gui.PanelContainer;
import java.util.logging.Level;
import javax.swing.SwingUtilities;

/**
 *
 * @author Gianni Van Hoecke <gianni.vh@gmail.com>
 */
public class SplashFrame extends javax.swing.JFrame implements LoadingUI
{
    private PanelContainer pc;

    /** Creates new form SplashFrame */
    public SplashFrame()
    {
        initComponents();
        DomainController.getInstance().setLoadingUI(this);
        new Thread()
        {
            @Override
            public void run()
            {
                pc = new PanelContainer();
                DomainController.getInstance().load();
            }
        }.start();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jProgressBar1 = new javax.swing.JProgressBar();
        infoPanel1 = new gui.help.about.InfoPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setAlwaysOnTop(true);
        setBackground(new java.awt.Color(255, 255, 255));
        setMinimumSize(new java.awt.Dimension(500, 200));
        setPreferredSize(new java.awt.Dimension(500, 200));
        setResizable(false);
        setUndecorated(true);

        jProgressBar1.setString("Loading...");
        jProgressBar1.setStringPainted(true);
        getContentPane().add(jProgressBar1, java.awt.BorderLayout.PAGE_END);
        getContentPane().add(infoPanel1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private gui.help.about.InfoPanel infoPanel1;
    private javax.swing.JProgressBar jProgressBar1;
    // End of variables declaration//GEN-END:variables

    public void updateUI(int progress, String text)
    {
        this.jProgressBar1.setValue(progress);
        this.jProgressBar1.setString(text);
    }

    public void setToLoadUI()
    {
        this.jProgressBar1.setIndeterminate(true);
        this.jProgressBar1.setString("Loading GUI...");
    }

    public void complete()
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                pc.setLocationRelativeTo(null);
                pc.setVisible(true);
                DomainController.getInstance().getLogger().log(Level.INFO, Settings.APP_NAME + " started.");
                close();
            }
        });
    }

    private void close()
    {
        this.dispose();
    }
}